package com.visikard.ui.fragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.visikard.application.Constant;
import com.visikard.ui.R;
import com.visikard.utility.Utils;

public class HistoryDateOptionFragment extends DialogFragment {

	public static final String KEY_SELECTED_OPTION = "key_selected_option";
	public static final String KEY_START_DATE = "key_start_date";
	public static final String KEY_END_DATE = "key_end_date";

	public interface IHistoryDateOption {
		public void onHistoryDateOptionDone(int options, Date from, Date to);
	}

	public static final int TODAY = 0;
	public static final int LAST_3_DAYS = 1;
	public static final int LAST_5_DAYS = 2;
	public static final int DATE_RANGE = 3;

	View optToday, optLast3Days, optLast5Days, optDateRange;
	ImageView imgToday, imgLast3Days, imgLast5Days, imgDateRange;
	View txtCancel, txtDone;
	TextView txtFrom, txtTo;

	int mSelectedOpt;
	Date mFrom, mTo;
	SimpleDateFormat mServerDateFormat = new SimpleDateFormat(Constant.TIME_FORMAT_STR_JUST_DATE);

	IHistoryDateOption mListener;

	public void setListener(IHistoryDateOption listener) {
		mListener = listener;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setStyle(STYLE_NO_TITLE, 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.history_trans_date_selection, container, false);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));

		optToday = view.findViewById(R.id.opt_today);
		optLast3Days = view.findViewById(R.id.opt_last_3day);
		optLast5Days = view.findViewById(R.id.opt_last_5day);
		optDateRange = view.findViewById(R.id.opt_date_range);
		txtFrom = (TextView) view.findViewById(R.id.txt_from);
		txtTo = (TextView) view.findViewById(R.id.txt_to);

		imgToday = (ImageView) view.findViewById(R.id.img_opt_today);
		imgLast3Days = (ImageView) view.findViewById(R.id.img_opt_last_3day);
		imgLast5Days = (ImageView) view.findViewById(R.id.img_opt_last_5day);
		imgDateRange = (ImageView) view.findViewById(R.id.img_opt_date_range);

		txtCancel = view.findViewById(R.id.txt_cancel);
		txtDone = view.findViewById(R.id.txt_done);

		optToday.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				clearOptions();
				setSelectedOption(TODAY);
			}
		});

		optLast3Days.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				clearOptions();
				setSelectedOption(LAST_3_DAYS);
			}
		});

		optLast5Days.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				clearOptions();
				setSelectedOption(LAST_5_DAYS);
			}
		});

		optDateRange.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				clearOptions();
				setSelectedOption(DATE_RANGE);
			}
		});

		txtCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		txtDone.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					if (mSelectedOpt == DATE_RANGE
							&& (mFrom == null || mTo == null || Utils.compareDateOnly(mFrom, mTo) > 0)) {
						Toast.makeText(getActivity(), getString(R.string.invalid_date_range), Toast.LENGTH_SHORT)
								.show();
						return;
					}
					mListener.onHistoryDateOptionDone(mSelectedOpt, mFrom, mTo);
				}
				dismiss();
			}
		});

		txtFrom.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doFromClicked();
			}
		});

		txtTo.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doToClicked();
			}
		});

		Bundle args = getArguments();
		if (args != null) {
			int selectedOption = args.getInt(KEY_SELECTED_OPTION, TODAY);
			setSelectedOption(selectedOption);
			if (selectedOption == DATE_RANGE) {
				long startDate = args.getLong(KEY_START_DATE);
				long endDate = args.getLong(KEY_END_DATE);
				if (startDate > 0 && endDate > 0) {
					mFrom = new Date(startDate);
					mTo = new Date(endDate);
					txtFrom.setText(mServerDateFormat.format(mFrom));
					txtTo.setText(mServerDateFormat.format(mTo));
				}
			}
		}
		else {
			setSelectedOption(TODAY);
		}

		return view;
	}

	protected void doFromClicked() {
		final Calendar c = Calendar.getInstance();
		final int year = c.get(Calendar.YEAR);
		final int month = c.get(Calendar.MONTH);
		final int day = c.get(Calendar.DAY_OF_MONTH);
		DatePickerDialog picker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.YEAR, year);
				cal.set(Calendar.MONTH, monthOfYear);
				cal.set(Calendar.DATE, dayOfMonth);
				mFrom = cal.getTime();
				txtFrom.setText(mServerDateFormat.format(mFrom));
			}
		}, year, month, day);
		picker.show();
	}

	protected void doToClicked() {
		final Calendar c = Calendar.getInstance();
		final int year = c.get(Calendar.YEAR);
		final int month = c.get(Calendar.MONTH);
		final int day = c.get(Calendar.DAY_OF_MONTH);
		DatePickerDialog picker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.YEAR, year);
				cal.set(Calendar.MONTH, monthOfYear);
				cal.set(Calendar.DATE, dayOfMonth);
				mTo = cal.getTime();
				txtTo.setText(mServerDateFormat.format(mTo));
			}
		}, year, month, day);
		picker.show();
	}

	private void clearOptions() {
		imgToday.setImageResource(R.drawable.check_box_c);
		imgLast3Days.setImageResource(R.drawable.check_box_c);
		imgLast5Days.setImageResource(R.drawable.check_box_c);
		imgDateRange.setImageResource(R.drawable.check_box_c);
		txtFrom.setEnabled(false);
		txtTo.setEnabled(false);
	}

	private void setSelectedOption(int option) {
		switch (option) {
			case TODAY:
				mSelectedOpt = TODAY;
				imgToday.setImageResource(R.drawable.check_icon_c);
				break;
			case LAST_3_DAYS:
				mSelectedOpt = LAST_3_DAYS;
				imgLast3Days.setImageResource(R.drawable.check_icon_c);
				break;
			case LAST_5_DAYS:
				mSelectedOpt = LAST_5_DAYS;
				imgLast5Days.setImageResource(R.drawable.check_icon_c);
				break;
			case DATE_RANGE:
				mSelectedOpt = DATE_RANGE;
				imgDateRange.setImageResource(R.drawable.check_icon_c);
				txtFrom.setEnabled(true);
				txtTo.setEnabled(true);
				break;
		}
	}

}
